package com.sql;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.javabean.BorrowBean;
import com.javabean.UserBean;

public class Borrow {

	public List query(String iduser){
		List list = new ArrayList();
		
		Connection connection = null;
		Statement statement = null;
		ResultSet rSet = null;
		String sql = "select * from borrow where iduser='"+iduser+"'";
		SqlManege sManege = new SqlManege();
		connection = sManege.doInit();
		try {
			statement = connection.createStatement();
			rSet = statement.executeQuery(sql);
			while(rSet.next()){
				BorrowBean bBean = new BorrowBean();
				bBean.setId(rSet.getString("id"));
				bBean.setIduser(rSet.getString("iduser"));
				bBean.setIdbook(rSet.getString("idbook"));
				/*bBean.setUsername(rSet.getString("username"));
				bBean.setBookname(rSet.getString("bookname"));*/
				bBean.setBtime(rSet.getString("b_date"));
				bBean.setRtime(rSet.getString("r_date"));
				list.add(bBean);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			rSet.close();
			statement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public List query(){
		List list = new ArrayList();
		
		Connection connection = null;
		Statement statement = null;
		ResultSet rSet = null;
		String sql = "select * from borrow ";
		SqlManege sManege = new SqlManege();
		connection = sManege.doInit();
		try {
			statement = connection.createStatement();
			rSet = statement.executeQuery(sql);
			while(rSet.next()){
				BorrowBean bBean = new BorrowBean();
				bBean.setIduser(rSet.getString("iduser"));
				bBean.setId(rSet.getString("id"));
				bBean.setIdbook(rSet.getString("idbook"));
				/*bBean.setUsername(rSet.getString("username"));
				bBean.setBookname(rSet.getString("bookname"));*/
				bBean.setBtime(rSet.getString("b_date"));
				bBean.setRtime(rSet.getString("r_date"));
				list.add(bBean);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			rSet.close();
			statement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public void doBorrow(String idbook,String iduser){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
		java.util.Date du = new java.util.Date();  
        String time = sdf.format(du);
		
        Connection connection = null;
		Statement statement = null;
		String sql1 = "insert into borrow (iduser,idbook,b_date) values('"+iduser+"','"+idbook+"','"+time+"')";
		String sql2 = "update book set store = store - 1 where idbook = '"+idbook+"'";
		SqlManege sManege = new SqlManege();
		connection = sManege.doInit();
		try {
			statement = connection.createStatement();
			statement.executeUpdate(sql1);
			statement.executeUpdate(sql2);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sManege.dbclose(connection, statement);
		
	}
	public void doReturn(String id,String book){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
		java.util.Date du = new java.util.Date();  
        String time = sdf.format(du);
		
        Connection connection = null;
		Statement statement = null;
		String sql1 = "update borrow set r_date='"+time+"' where id = '"+id+"'";
		String sql2 = "update book set store = store + 1 where idbook = '"+book+"'";
		SqlManege sManege = new SqlManege();
		connection = sManege.doInit();
		try {
			statement = connection.createStatement();
			statement.executeUpdate(sql1);
			statement.executeUpdate(sql2);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sManege.dbclose(connection, statement);
		
	}
}
